package com.stu.manager.organization.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.stu.manager.config.Result;
import com.stu.manager.organization.entity.SysDepartment;
import com.stu.manager.organization.entity.SysProfessional;
import com.stu.manager.organization.entity.dto.ProfessionalDto;
import com.stu.manager.organization.mapper.SysProfessionalMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-02-12
 */
@Service
public class SysProfessionalServiceImpl extends ServiceImpl<SysProfessionalMapper, SysProfessional> implements IService<SysProfessional> {

    public IPage<SysProfessional> pageProfessional(Page<SysProfessional> pageProfessional, QueryWrapper<SysProfessional> wrapper) {
        IPage<SysProfessional> sysProfessionalIPage = baseMapper.selectPage(pageProfessional, wrapper);
        return sysProfessionalIPage;
    }

    public Result addProfessional(ProfessionalDto professionalDto) {
        String professionalCode = professionalDto.getProfessionalCode();
        QueryWrapper<SysProfessional> wrapper = new QueryWrapper<>();
        wrapper.eq("professional_code",professionalCode);
        Integer count = baseMapper.selectCount(wrapper);
        if(count>0){
            return Result.error().message("当前专业代码已存在，请修改");
        }
        SysProfessional sysProfessional = new SysProfessional();
        BeanUtils.copyProperties(professionalDto,sysProfessional);
        int insert = baseMapper.insert(sysProfessional);
        if(insert>0){
            return Result.ok().message("添加成功");
        }else {
            return Result.error().message("添加失败");
        }
    }

    //修改专业
    public Result updateProfessional(SysProfessional sysProfessional) {

        QueryWrapper<SysProfessional> wrapper = new QueryWrapper<>();
        wrapper.eq("professional_code",sysProfessional.getProfessionalCode()).ne("id",sysProfessional.getId());
        Integer count = baseMapper.selectCount(wrapper);
        if(count>0){
            return Result.error().message("专业代码重复，请修改");
        }

        int insert = baseMapper.updateById(sysProfessional);
        if(insert>0){
            return Result.ok().message("修改成功");
        }else {
            return Result.error().message("修改失败");
        }

    }

    //查询专业
    public SysProfessional getProfessionalByCode(String professionalCode) {
        QueryWrapper<SysProfessional> wrapper = new QueryWrapper<>();
        wrapper.eq("professional_code",professionalCode);
        SysProfessional sysProfessional = baseMapper.selectOne(wrapper);
        return sysProfessional;
    }
}
